package com.wjggy.core.common.util.preferences;


import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.wjggy.core.common.springview.ViewUtil;
import com.wjggy.core.common.util.filter.CommsFilter;
import com.wjggy.core.exception.WjggyException;


/**
 * 存储配置文件中得变量 
 * 如果变量值传入${....}标示在配置文件中读取改变量的值如：configPath=${web.dir}/webapp ，${web.dir}就是读取在configPath之前定义的web.dir的变量值
 * 
 * @author xiaofan.yu
 * @version SystemGlobals.java Nov 8, 2011 8:08:12 PMZ
 */
public class SystemGlobals implements VariableStore{
	private static SystemGlobals globals = new SystemGlobals();
	private String defaultConfig;
	private Properties defaults = new Properties();
	private VariableExpander expander = new VariableExpander(this, "${", "}");
	private static final Logger logger = Logger.getLogger(SystemGlobals.class);
	private SystemGlobals() {}
	public static void initGlobals(String appPath, String mainConfigurationFile)
	{
		globals = new SystemGlobals();
		globals.buildSystem(appPath, mainConfigurationFile);
		ViewUtil.style="/"+globals.getValue(ConfigKeys.WEB_STYLE)+"/";
		CommsFilter.charset=globals.getValue(ConfigKeys.APPLICATION_CHARSET);
	}
	

	/**
	 * 进入启动项
	 * @param appPath
	 * @param mainConfigurationFile
	 */
	private void buildSystem(String appPath, String mainConfigurationFile)
	{
		if (mainConfigurationFile == null) {
			throw new InvalidParameterException("defaultConfig could not be null");
		}
		this.defaultConfig = mainConfigurationFile;
		this.defaults = new Properties();
		this.defaults.put(ConfigKeys.APPLICATION_PATH, appPath);
		this.defaults.put(ConfigKeys.DEFAULT_CONFIG, mainConfigurationFile);
		SystemGlobals.loadDefaults();
		
	}
	public static void loadDefaults()
	{
		try
		{
			FileInputStream input = new FileInputStream(globals.defaultConfig);
			globals.defaults.load(input);
			for(Object key:globals.defaults.keySet()){
				logger.info("加载配置键值对:"+key+"----->"+globals.defaults.getProperty(key.toString()));
			}
			input.close();
		}
		catch (IOException e)
		{	
			logger.error(e);
			throw new WjggyException(e);
		}
	}
	
	
	/**
	 * 清空
	 */
	public static void reset()
	{
		globals.defaults.clear();
	}

	public static String getValue(String field)
	{
		return globals.getVariableValue(field);
	}
 
	public static int getIntValue(String field)
	{
		return Integer.parseInt(getValue(field));
	}
	 
	public static boolean getBoolValue(String field)
	{
		return "true".equals(getValue(field));
	}

	 
	public String getVariableValue(String field)
	{
		String preExpansion = this.defaults.getProperty(field);
		if (preExpansion == null) {
				return null;
		}
		return expander.expandVariables(preExpansion);
	}

	public static String getApplicationPath()
	{
		return getValue(ConfigKeys.APPLICATION_PATH);
	}

	public static String getApplicationResourceDir()
	{
		return getValue(ConfigKeys.RESOURCE_DIR);
	}
}
